package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.model.TrainDataBean;
import com.zahb.qadx.ui.activity.CommentTeacherActivity;
import com.zahb.qadx.ui.activity.OldTrainDetailsActivity;
import com.zahb.qadx.ui.activity.TrainDetailsActivity;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.List;


public class MyTrainAdapter extends RecyclerView.Adapter<MyTrainAdapter.MyTrainViewHodler> {
    private Context mContext;
    private List<TrainDataBean.DataBean> dataBeanList;
    public MyTrainAdapter(Context mContext, List<TrainDataBean.DataBean> dataBeanList) {
        this.mContext = mContext;
        this.dataBeanList = dataBeanList;
    }

    @NonNull
    @Override
    public MyTrainViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyTrainViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_list_train, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyTrainViewHodler holder, int position) {
        //holder.img_pic.setImageResource(dataBeanList.get(position).getPictureUrl());
        String imgpic = dataBeanList.get(position).getPictureUrl();
        int classid = dataBeanList.get(position).getId();

        if (TextUtils.isEmpty(imgpic)){
            holder.img_pic.setImageResource(R.drawable.ic_train_icon);
        }else {
            ImageLoaderKt.loadImage(holder.img_pic,imgpic);
        }
        int way = dataBeanList.get(position).getWay();
        if (way == 1){
            holder.tv_way.setVisibility(View.VISIBLE);
            holder.tv_way.setText("线上");
        }else if (way == 2){
            holder.tv_way.setVisibility(View.VISIBLE);
            holder.tv_way.setText("面授");
        } else if (way == 0){
            holder.tv_way.setVisibility(View.GONE);
        }



        holder.tv_name.setText(dataBeanList.get(position).getClassName());
        String introduction =dataBeanList.get(position).getBriefIntroduction();
        holder.tv_title.setText(introduction.equals("") ? "暂无简介" : introduction);

        holder.tvState.setText(getState(dataBeanList.get(position).getClassStatus()));
        if(dataBeanList.get(position).getClassStatus() == 4){
            holder.tvState.setTextColor(mContext.getResources().getColor(R.color.train_did));
            holder.tvState.setBackgroundResource(R.drawable.train_state_did);
        }else{
            holder.tvState.setTextColor(mContext.getResources().getColor(R.color.train_undo));
            holder.tvState.setBackgroundResource(R.drawable.train_state_undo);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TrainDetailsActivity.class);
                if(way==1){
                    intent = new Intent(mContext, OldTrainDetailsActivity.class);
                }
                Bundle bundle = new Bundle();
                bundle.putInt("id",classid);
                bundle.putInt("type",way);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        if(dataBeanList.get(position).getClassStatus() == 4){
            holder.tvComment.setVisibility(View.VISIBLE);
        }else{
            holder.tvComment.setVisibility(View.INVISIBLE);
        }
        if (way == 0){
            holder.tvComment.setVisibility(View.INVISIBLE);
        }
        holder.tvComment.setText(dataBeanList.get(position).evaluationStatus?"已评价":"去评价");

        if(dataBeanList.get(position).evaluationStatus){
            holder.tvComment.setTextColor(mContext.getResources().getColor(R.color.train_did));
            holder.tvComment.setBackgroundResource(R.drawable.comment);
        }else{
            holder.tvComment.setTextColor(mContext.getResources().getColor(R.color.blue));
            holder.tvComment.setBackgroundResource(R.drawable.commenting);
        }



        holder.tvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, CommentTeacherActivity.class);
                intent.putExtra("type",dataBeanList.get(position).evaluationStatus);
                intent.putExtra("data",dataBeanList.get(position).getId());
                intent.putExtra("datatwo",dataBeanList.get(position).getClassName());
                ((AppCompatActivity)mContext).startActivityForResult(intent,110);
            }
        });

    }

    public String getState(int state){
        if(state==1){
            return "未发布";
        }
        if(state==2){
            return "未开始";
        }
        if(state==3){
            return "进行中";
        }
        if(state==4){
            return "已结束";
        }
        return "已关闭";
    }

    @Override
    public int getItemCount() {
        return dataBeanList !=null ? dataBeanList.size() : 0;
    }

    static class MyTrainViewHodler extends RecyclerView.ViewHolder {
        private ImageView img_pic;
        private TextView tv_name, tv_title,tv_way,tvComment,tvState;

        public MyTrainViewHodler(@NonNull View itemView) {
            super(itemView);
            img_pic = itemView.findViewById(R.id.iv_train_icon);
            tv_name = itemView.findViewById(R.id.tv_classname);
            tv_title = itemView.findViewById(R.id.tv_train_briefIntroduction);
            tv_way = itemView.findViewById(R.id.tv_train_way);
            tvComment = itemView.findViewById(R.id.tvComment);

            tvState = itemView.findViewById(R.id.tvState);
        }
    }
}
